package hk.hku.hooku.servlet;

import hk.hku.hooku.db.ContactDB;
import hk.hku.hooku.db.EventDB;
import hk.hku.hooku.pojo.Event;
import hk.hku.hooku.pojo.EventChoice;
import hk.hku.hooku.pojo.EventResult;
import hk.hku.hooku.pojo.User;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.Timestamp;

/**
 * Servlet implementation class AddEvent
 */
public class AddEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("in AddEventServlet");
		
		String pemail=request.getParameter("pemail");
		String title=request.getParameter("title");
		String location=request.getParameter("location");
		String description=request.getParameter("description");
		String createtime = request.getParameter("createtime");
		createtime = createtime.replace('*', ' ');
		String eventurl=request.getParameter("eventurl");
		String eventchoices = request.getParameter("eventchoices");
		
		System.out.println("pemail: "+pemail);
		System.out.println("title: "+title);
		System.out.println("location: "+location);
		System.out.println("description: "+description);
		System.out.println("createtime: "+createtime);
		System.out.println("eventurl: "+eventurl);
		
		String[] choices=eventchoices.split(",");
		
		User proposer = null;
		int  proposerid = ContactDB.getProposerByEmail(pemail);
		System.out.println("pid: "+proposerid);
		
		ArrayList<EventChoice> event_choices=new ArrayList<EventChoice>();
		for(int i = 0; i < choices.length; i++){
			EventChoice choice=new EventChoice();
			choice.setId(i);
			String[] time = choices[i].split("\\+");
			if(time.length!=3)
				continue;
			
			String start = time[0]+" "+time[1]+":00";
			String end   = time[0]+" "+time[2]+":00";
			
			System.out.println(start+"~"+end);
			
			Timestamp starttime = Timestamp.valueOf(start);
			Timestamp endtime = Timestamp.valueOf(end);
			choice.setStartTime(starttime);
			choice.setEndTime(endtime);
			event_choices.add(choice);
		}
		
		Timestamp createstamp = Timestamp.valueOf(createtime);
		Timestamp closeTime=new Timestamp(0);
		
		Event event = new Event(eventurl, title, location, description, proposer, 0, createstamp, closeTime, pemail, event_choices);
		int eventid = EventDB.addEvent(event, proposerid);
		boolean is_success = EventDB.addEventChoice(event_choices, eventid);
		is_success = is_success && (EventDB.addHistory(proposerid, eventid));
		PrintWriter writer=response.getWriter();
		if(is_success){
			writer.write("success");
		}else{
			writer.write("fail");
		}
	}
}
